<template>
    <div class="login">
        <div class="form" :model="user">
            <el-form label-width="80px" :model="user" size="small" :rules="loginRules" ref="save">
  <el-form-item label="用户名：" prop="account">
    <el-input type="text" v-model="user.account" >
        <i slot="prefix" class="iconfont icon-yonghufill"></i>
    </el-input>
  </el-form-item>
  <el-form-item label="密码：" prop="password">
    <el-input :type="flag?'text':'password'" v-model="user.password">
        <i slot="prefix" class="iconfont icon-mima"></i>
        <i slot="suffix" @click="flag=!flag" :class="['yanjing','iconfont',flag?'icon-yanjing_huaban1':'icon-05wangluoanquan_biyanjing']"></i>
    </el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="goLogin">提交</el-button>
  </el-form-item>
</el-form>
        </div>
    </div>
</template>

<script>
import {api_login} from '../../api/users';
import {headler} from '../../router/index';
    export default {
        data() {
            return {
                user:{
                    account:"",
                password:""
                },
                flag:false,
                loginRules:{
                    account:[
                        {required:true,message:"请输入用户名",trigger:'blur'},
                        {min:4,max:12,message:"请输入4-12字符",trigger:'blur'}
                    ],
                    password:[
                        {required:true,message:"请输入密码",trigger:'blur'},
                    ]
                }
            };
        },
        methods: {
            goLogin() {
                // $refs,全部表单验证
                this.$refs.save.validate(async (a)=>{
                   let rst= await api_login(this.user);
                   let{code,msg,token,role}=rst;
                   if(code==0){
                          //    用户登陆成功传值role，中包含super,和normal
                       this.$session.set("sell_role",role);
                    //    调用计算路由方法
                               headler();
                       this.$session.set("sell_token",token);
                       this.$router.replace("/home");
               
                   }else{
                       this.$message.Error(msg);
                   }
                   console.log(rst);
                })
            },
        },
    }
</script>

<style lang="less" scoped>
.login{
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #243E49;
    
    .form{
        width: 340px;
        height: 260px;
        // border: 1px solid red;
        /deep/.el-form-item__label{
            color: white;
            
        }
        .yanjing{
                font-size: 27px;
            }
            button{
                width: 100%;
            }
            /deep/.el-form-item__content{
                margin: 5px 0;
            }
    }
    h2{
        font-size: 22px;
        text-align: center;
        margin: 20px 0;
        color: white;
    }
}
</style>